package com.ccx.coachsite.task;

import java.io.File;
import java.net.URLEncoder;

import org.json.JSONObject;

import android.content.Context;

import com.ccx.coachsite.bus.Account;
import com.ccx.coachsite.bus.AccountMgr;
import com.ccx.coachsite.bus.ILoadBus;
import com.ccx.coachsite.bus.LoadDataBus;
import com.ccx.coachsite.http.BaseResp;
import com.ccx.coachsite.http.HttpCallBack;
import com.ccx.coachsite.http.HttpErrorDesc;
import com.ccx.coachsite.http.HttpRequest;
import com.ccx.coachsite.http.UrlConfig;
import com.ccx.coachsite.util.Log;
import com.ccx.coachsite.util.StringUtil;

public class ReinfoTask extends TaskBase implements ILoadBus{

	private Account mAccount;
	//private Context mContext;
	private String fileIconName;
	private boolean isLoadComed = false;
	
	public ReinfoTask(Context context, HttpCallBack<BaseResp> call, Account account,String fileName) {
		super(call, context);
		mAccount = account;
		this.fileIconName = fileName;
		isLoadComed = false;
	}
	
	protected void onDataParse(String responseString)
	{
		try {
			JSONObject jsonObject = new JSONObject(responseString);
			JSONObject respsonObject = jsonObject.getJSONObject("response");
			if(respsonObject == null)
				return;
			
			mAccount.setUsername(respsonObject.getString("username"));
			mAccount.setOwerIcon(respsonObject.getString("icon"));
			mAccount.setMobile(respsonObject.getString("mobile"));
			mAccount.setUserId(respsonObject.getInt("id"));
			AccountMgr.saveAccountInfo(mContext, mAccount);
			
			
		} catch (Exception e) {
			Log.e(e.getMessage(), e);
		} finally {
		}
	}
	
	@Override
	protected BaseResp doInBackground(Integer... params) {
		HttpRequest request = new HttpRequest(BaseResp.class);
		
		BaseResp resp = new BaseResp();
		try {
			
			String filePath = fileIconName;
			String fileName = "";
			if(!StringUtil.isEmpty(fileIconName))
			{
				fileName = filePath.substring(filePath.lastIndexOf('/')+1);
			}
			
			
			request.addParameter("mobile", mAccount.getMobile());
			String userName = URLEncoder.encode(mAccount.getUsername(), "UTF-8");
			String url = UrlConfig.getUrlByKey(UrlConfig.ReinfoUrlKey) + "?userId=" + mUserId
					+"&userType="+mUserType+"&userName="+userName;
			
			
			if(!StringUtil.isEmpty(fileIconName))
				url += "&iconType=1";
			else
				url += "&iconType=0";
			
			//if(!StringUtil.isEmpty(mAccount.getQq()))
			url += "&QQ="+mAccount.getQq();
			//request.addParameter("userName", mAccount.getUsername());
			String res = request.uploadFile(url, fileIconName,fileName);
			
			if (res != null) {
				String result = res;
				JSONObject jsonObject = new JSONObject(result);
				String status = jsonObject.getString("code");
				String desc = jsonObject.getString("desc");
				resp.setDesc(desc);
				if(status.equals("200"))
				{
					resp.setResponse(result);
					onDataParse(result);
					resp.setStatus(BaseResp.SUCCESS);
					
					LoadDataBus loadData = new LoadDataBus(mContext, this);
					loadData.loadData(mAccount.getMobile());
					
					while(!isLoadComed)
					{
						try
						{
							Thread.sleep(500);
						}catch (Exception e) 
						{
							
						}
					}
				}
				else
				{
					int statusCode = Integer.parseInt(status);
					HttpErrorDesc.setErrorDesc(statusCode, resp);
					resp.setStatus(BaseResp.ERROR);
				}
			}
		} catch (Exception e) {
			resp.setStatus(BaseResp.ERROR);
			Log.e(e.getMessage(), e);
		} finally {
			request.release();
		}

		return resp;
	}

	@Override
	protected String genUrl(HttpRequest request)
	{
		String url = UrlConfig.getUrlByKey(UrlConfig.ReinfoUrlKey) + "?userId" + mUserId
				+"&userType="+mUserType+"&userName="+mAccount.getUsername();
		
		/*
		HttpClient client = new DefaultHttpClient();
		HttpPost httpPost = new HttpPost(url);
		HttpResponse response = null;
		try {
			File f = new File(mAccount.getOwerIcon());
			FileEntity entity = new FileEntity(f, "binary/octet-stream");
			httpPost.setEntity(entity);
			response = client.execute(httpPost);
		} catch (Exception e) {
		System.out.println(e.toString());
		} finally {
		}
		*/
		return UrlConfig.getUrlByKey(UrlConfig.RegisterUrlKey);
	}

	@Override
	public void onCompleted(boolean isSuccess, String desc) {
		
		isLoadComed = true;
	}
}